package com.disease.api.service.web.hospital;

import com.disease.api.service.entity.HospitalInfo;
import com.disease.api.service.mapper.HospitalInfoMapper;
import com.disease.api.service.model.ResponseResult;
import com.disease.api.service.service.DeathCardReportService;
import com.disease.api.service.service.DeathInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@Slf4j
@RestController
@RequestMapping(value = "/disease/v1/api/hospital")
public class HospitalController {

    @Autowired
    HospitalInfoMapper hospitalInfoMapper;

    @Autowired
    DeathCardReportService deathCardReportService;

    @Autowired
    DeathInfoService deathInfoService;

    @ResponseBody
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public ResponseResult<HospitalInfo> queryHospitalInfo(){
        return ResponseResult.success(hospitalInfoMapper.selectOne());
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResponseResult editHospitalInfo(@RequestBody HospitalInfo hospitalInfo){
        try {
            if(Optional.ofNullable(hospitalInfo.getId()).isPresent()){
                hospitalInfoMapper.updateHospitalInfo(hospitalInfo);
            }else {
                hospitalInfoMapper.insert(hospitalInfo);
            }
            deathCardReportService.updateHospitalInfo(hospitalInfo);
            deathInfoService.updateHospitalInfo(hospitalInfo);
            return ResponseResult.success("更新成功");
        }catch (Exception e){
            log.error("更新失败:{}",e.getMessage());
            return ResponseResult.fail("更新失败");
        }
    }

}
